﻿namespace Yom.Data
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    /// <summary>
    /// 章节数据操作
    /// 
    /// 创建: 邱柯皓 2009/01/20 13:51
    /// </summary>
    public class Chapter
    {
        #region 私有成员定义
        private readonly DBHelper db = new DBHelper();
        #endregion

        #region 构造函数
        public Chapter() { }
        #endregion

        #region 私有方法
        /// <summary>
        /// 通过DataReader获取章节实体对象
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:53
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回章节实体对象</returns>
        private Yom.Model.Chapter GetModel(SqlDataReader dr)
        {
            Yom.Model.Chapter model = new Yom.Model.Chapter();
            model.ChapterID = db.GetLong(dr["ChapterID"]);
            model.ImgPath = db.GetString(dr["ImgPath"]);
            model.OrderID = db.GetInt(dr["OrderID"]);
            model.VolumeID = db.GetLong(dr["VolumeID"]);

            return model;
        }

        /// <summary>
        /// 通过DataReader获取章节列表
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:55
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回章节列表</returns>
        private List<Yom.Model.Chapter> GetList(SqlDataReader dr)
        {
            List<Yom.Model.Chapter> list = new List<Yom.Model.Chapter>();

            while (dr.Read())
                list.Add(GetModel(dr));

            return list;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 添加章节
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:56
        /// </summary>
        /// <param name="model">章节实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Add(Yom.Model.Chapter model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("INSERT INTO [Yom_Chapter] (");
            strSQL.Append("[VolumeID], [ImgPath], [OrderID]");
            strSQL.Append(") VALUES (");
            strSQL.Append("@VolumeID, @ImgPath, @OrderID)");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@VolumeID", DbType.Int64, model.VolumeID),
                db.CreateInSqlParameter("@ImgPath", DbType.String, model.ImgPath),
                db.CreateInSqlParameter("@OrderID", DbType.Int32, model.OrderID)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 修改章节
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:59
        /// </summary>
        /// <param name="model">章节实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Update(Yom.Model.Chapter model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE [Yom_Chapter] SET");
            strSQL.Append("[VolumeID]=@VolumeID, [ImgPath]=@ImgPath, [OrderID]=@OrderID");
            strSQL.Append(" WHERE [ChapterID]=@ChapterID");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@VolumeID", DbType.Int64, model.VolumeID),
                db.CreateInSqlParameter("@ImgPath", DbType.String, model.ImgPath),
                db.CreateInSqlParameter("@OrderID", DbType.Int32, model.OrderID),
                db.CreateInSqlParameter("@ChapterID", DbType.Int64, model.ChapterID)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 删除章节
        /// 
        /// 创建: 邱柯皓 2009/01/20 14:02
        /// </summary>
        /// <param name="ChapterID">章节编号</param>
        /// <returns>返回成功标识</returns>
        public bool Delete(long ChapterID)
        {
            string strSQL = "DELETE FROM [Yom_Chapter] WHERE [ChapterID]=@ChapterID";
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@ChapterID", DbType.Int64, ChapterID) };

            return db.ExecuteNonQuery(CommandType.Text, strSQL, cmdParms) > 0;
        }

        /// <summary>
        /// 获取章节实体对象
        /// 
        /// 创建: 邱柯皓 2009/01/20 14:04
        /// </summary>
        /// <param name="ChapterID">章节编号</param>
        /// <returns>返回章节实体对象</returns>
        public Yom.Model.Chapter GetModel(long ChapterID)
        {
            string strSQL = string.Format("SELECT * FROM [Yom_Chapter] WHERE [ChapterID]={0}", ChapterID);

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                Yom.Model.Chapter model = new Yom.Model.Chapter();

                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 获取指定分卷下的章节列表
        /// 
        /// 创建: 邱柯皓 2009/01/20 14:06
        /// </summary>
        /// <param name="VolumeID">分卷编号</param>
        /// <returns>返回章节列表</returns>
        public List<Yom.Model.Chapter> GetList(long VolumeID)
        {
            string strSQL = string.Format("SELECT * FROM [Yom_Chapter] WHERE [VolumeID]={0} ORDER BY [OrderID] ASC", VolumeID);

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                List<Yom.Model.Chapter> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 分页获取分卷下章节列表
        /// 
        /// 创建: 邱柯皓 2009/03/11 14:50
        /// </summary>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="volumeID">分卷编号</param>
        /// <param name="sort">排序方式, true为正序, false为倒序</param>
        /// <returns>返回分卷列表</returns>
        public List<Yom.Model.Chapter> GetList(int pageSize, int pageIndex, long volumeID, bool sort)
        {
            using (SqlDataReader dr = db.GetPager(pageSize, pageIndex, "[Yom_Chapter]", "[OrderID]", "[VolumeID]=" + volumeID.ToString(), sort))
            {
                List<Yom.Model.Chapter> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 修改章节排序
        /// 
        /// 创建: 邱柯皓 2009/01/20 14:08
        /// </summary>
        /// <param name="ChapterID">章节编号</param>
        /// <param name="OrderID">排序编号</param>
        /// <returns>返回成功标识</returns>
        public bool Order(long ChapterID, int OrderID)
        {
            Yom.Model.Chapter model = GetModel(ChapterID);
            model.OrderID = OrderID;

            return Update(model);
        }

        /// <summary>
        /// 获取分卷下的章节数量
        /// 
        /// 创建: 邱柯皓 2009/03/10 12:23
        /// </summary>
        /// <param name="VolumeID">分卷编号</param>
        /// <returns>返回章节数量</returns>
        public int GetCount(long VolumeID)
        {
            string strSQL = "SELECT COUNT(*) FROM [Yom_Chapter] WHERE [VolumeID]=" + VolumeID;
            object m = db.ExecuteScalar(CommandType.Text, strSQL, null);
            return db.GetInt(m);
        }
        #endregion
    }
}